
//Projects Directive
dmdPMApp.directive('projectlist', ['projTabsService', function(projTabsService){
	return {
		restrict:"E",
		scope:{},
		templateUrl:"static/partials/project/projectlist-directive.html",
		controller:function ($scope, $attrs) {
			$scope.openSite = function (id) {
				projTabsService.addTab("Site :" + id, "sites.html");
			};
			$scope.editProject = function (projName, projectId) {
			  var projectDetailsTpl = "<createproject projid=" + projectId + "></createproject>";
				projTabsService.addTab("Edit Project:" + projName, projectDetailsTpl);
			};
			$scope.viewProjectDetails = function (projName, projectId) {
        		var projectDetailsTpl = "<projdetails projid=" + projectId + "></projdetails>";
				projTabsService.addTab(projName, projectDetailsTpl);
			};
		}
	};
}]);



//Create Project Directive. Reused for Edit Project as well.
dmdPMApp.directive('createproject', ['projTabsService', function(projTabsService){
	return {
		restrict:"E",
		scope:{},
		templateUrl:"static/partials/project/createproject-directive.html",
		link: function(scope, element, attrs){
			scope.project = {id:"To be generated", name:"", serviceRequestId:"", custId:"", status:"New", description:"", plannedStartDate:"", plannedEndDate:"", actualStartDate:"",actualEndDate:"",managerRagStatus:"Green",ragStatus:"Green"};			
		}, 
		controller:function ($scope, $attrs,$http) {
			$scope.openProjectSite = function (projSiteName, projSiteId) {
				var projSiteTmpl = "<projectsite projsiteid='" + projSiteId + "'></projectsite>";
				projTabsService.addTab(projSiteName, projSiteTmpl);
			};
			$scope.create = function(project){
				project.id = "";
				$http.post("project/create", project).
					success(function(data, status) {
						$scope.status = status;
						$scope.project.id = data;
						alert("project saved");
					});
			};
		}
	};
}]);


//Create Project Directive. Reused for Edit Project as well.
dmdPMApp.directive('projdetails', ['projTabsService', function(projTabsService){
	return {
		restrict:"E",
		scope:{},
		templateUrl:"static/partials/project/projdetails-directive.html",
		controller:function ($scope, $attrs) {
			$scope.openProjectSite = function (projSiteName, projSiteId) {
				var projSiteTmpl = "<projectsite projsiteid='" + projSiteId + "'></projectsite>";
				projTabsService.addTab(projSiteName, projSiteTmpl);
			};
		}
	};
}]);